package com.itheima.app.manager;

import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Sound;
import com.itheima.domain.vo.SoundVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class SoundManager {

    @Reference
    SoundService soundService;
    @Reference
    UserInfoService userInfoService;
    @Autowired
    FastFileStorageClient client;
    @Autowired
    FdfsWebServer server;;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    //存储录音
    public void save(MultipartFile sound) throws IOException {
        Long userId= UserHolder.get().getId();

        UserInfo userInfo=userInfoService.findById(userId);
        //使用fastfds存储录音
        StorePath storePath = client.uploadFile(sound.getInputStream(), sound.getSize(), FileUtil.extName(sound.getOriginalFilename()), null);
        String soundUrl = server.getWebServerUrl()+storePath.getFullPath();

        Sound sound1=new Sound();
        sound1.setUserId(userId);
        sound1.setSoundUrl(soundUrl);
        sound1.setGender(userInfo.getGender());


       soundService.save(sound1);


    }

    /**
     * 接受语音
     * @return
     */

    public ResponseEntity getSound()
    {
        /*返回一个对象*/
        Long userId=UserHolder.get().getId();


        //根据id查询这个人的信息
        UserInfo userInfo=userInfoService.findById(userId);
        Integer times=soundService.findTimeById(userId);
        //做判断次数还有几次
        if (times==0)
        {
            //提示用户次数用尽
           return ResponseEntity.status(404).body("次数用尽");
        }else {
            //返回一个声音，并将这个用户的机会减一
            Sound sound= soundService.getSounds( userId,userInfo.getGender());

           //开始封住SoundVo

            UserInfo userInfo1=userInfoService.findById(sound.getUserId());
            SoundVo soundVo=new SoundVo();
            Long Ids=sound.getUserId();
            Integer ID=Ids.intValue();
            soundVo.setId(ID);
            soundVo.setAge(userInfo1.getAge());
            soundVo.setAvatar(userInfo1.getAvatar());
            soundVo.setGender(userInfo1.getGender());
            soundVo.setNickname(userInfo1.getNickname());
            soundVo.setSoundUrl(sound.getSoundUrl());
            soundVo.setRemainingTimes(soundService.findTimeById(userId));

            return ResponseEntity.ok(soundVo);
        }
    }
}
